﻿using System;

public class TestAlgorithm
{
    private delegate double GeneralFunction(double x1, double x2 = 0, double x3 = 0);

	public TestAlgorithm()
	{
	}

    //run simple test
    public double CartesianX()
    {
        //get the Ellipsoid flattening
        //f1 = 0.003407546284
        //a1 = 6378249.145
        //a2 = 6378137.000
        //f2 = 0.00352810665
        double f = 0.00352810665;
        double a = 6378137.000; //6378249.145;
        
        double e = f * (2 - f);
        e = Math.Sqrt(e);
        
        double v = 0.0;
        double latitude = 9.319180833333;
        double longitude = 12.2305902777778;
        double height = 268.127;

        //calculate the v
        /*
        GeneralFunction fxV = (double majorAxis, double eccentricity, double latitudeDegree) =>
        {
            
            double t = latitudeDegree;            
            //get square of e
            double eSqr = Math.Pow(eccentricity, 2);
            Console.WriteLine("Eccentricity Square= {0}", eSqr);
            //get square of sin latitude
            double sinT = Math.Sin(t);
            Console.WriteLine("Sin latitude = {0}", sinT);
            double sinTSqr = Math.Pow(sinT, 2);
            Console.WriteLine("Sin latitude squared = {0}", sinTSqr);
            //get the product of eSqr and sinTSqr
            double denum = eSqr * sinTSqr;
            denum = 1 - denum;
            denum = Math.Sqrt(denum);
            Console.WriteLine("Denuminator = {0}", denum);
            //calculate v
            double verticalPrime = majorAxis / denum;
            Console.WriteLine("The Vertical Prime = {0}", verticalPrime);
            return verticalPrime;
        };
        */

        //convert values to radians
        double latitudeRad = latitude * Math.PI / 180;
        double longitudeRad = longitude * Math.PI / 180;

        double cosLatitudeRad = Math.Cos(latitudeRad);
        double cosLongitudeRad = Math.Cos(longitudeRad);
        double sinLongitudeRad = Math.Sin(longitudeRad);

        cosLongitudeRad = Math.Round(cosLongitudeRad, 9);
        cosLatitudeRad = Math.Round(cosLatitudeRad, 9);
        sinLongitudeRad = Math.Round(sinLongitudeRad, 10);

        double eSqr = Math.Pow(e, 2);
        eSqr = Math.Round(eSqr, 12);

        double sinSqrLatitude = Math.Pow(Math.Sin(latitudeRad), 2);
        sinSqrLatitude = Math.Round(sinSqrLatitude, 9);
        double e2sin2 = eSqr * sinSqrLatitude;

        double denuminator = Math.Sqrt((1 - e2sin2));
        denuminator = Math.Round(denuminator, 9);
        

        v = a / denuminator;

        Console.WriteLine("cos latitude {0}", cosLatitudeRad);
        Console.WriteLine("cos longitude {0}", cosLongitudeRad);
        Console.WriteLine("sin longitude {0}", sinLongitudeRad);
        Console.WriteLine("e2 {0} ", eSqr);
        Console.WriteLine("sin2latitude {0} ", sinSqrLatitude);
        Console.WriteLine("e2sin2 {0}  ", e2sin2);
        Console.WriteLine("Denominator {0}", denuminator);       
        Console.WriteLine("v {0}", v);
        

               
        double x = (v + height) * Math.Cos(latitudeRad) * Math.Cos(longitudeRad);
        Console.WriteLine("Math.PI = {0}", Math.PI);
        Console.WriteLine("Cosine of latitude = {0}", Math.Cos(latitudeRad));
        Console.WriteLine("Cosine of longitude = {0}", Math.Cos(longitudeRad));
        return x;
    }

    public double CartesianY()
    {
        return 0.0;
    }
}
